//
//  MapView.swift
//  Landmarks
//
//  Created by Hello Cai on 2020/4/3.
//  Copyright © 2020 Hello Cai. All rights reserved.
//

import SwiftUI
import MapKit

//static mode 时仅显示 SwiftUI 视图，MKMapView是UIView视图，需要切换到实时模式才能看到地图

/*遵循UIViewRepresentable协议
  该协议需要实现两个方法：makeUIView和updateUIView
 */
struct MapView: UIViewRepresentable {
//    var body: some View {
//        Text("Hello, World!")
//    }
    //提供参数
    var coordinate: CLLocationCoordinate2D
    
    //makeUIView(context:)替换body属性，创建并返回空的MKMapView
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }
    
    func updateUIView(_ view:MKMapView, context: Context) {
//        let coordinate = CLLocationCoordinate2D(latitude: 34.011286, longitude: -116.166868)
//        let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
        
        let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        view.setRegion(region, animated: true)
    }
}

struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView(coordinate: landmarkData[0].locationCoordinate)
    }
}
